//
//  AddStoreNumView.m
//  Yanwei
//
//  Created by Seven-Augus on 2017/4/21.
//  Copyright © 2017年 DCloud. All rights reserved.
//

#import "AddStoreNumView.h"
#import "UIView+Util.h"

#define TextfieldBackGroundColor Color140

//#define maxNum 999
#define maxNum [_inventory integerValue]

typedef NS_ENUM(NSInteger, addStoreButtonType) {
    reduceNumButtonType, // 减少按钮
    addNumButtonType // 增加按钮
};

@interface AddStoreNumView () <UITextFieldDelegate>

@property (nonatomic, strong) UILabel *lineLabel; // 线
@property (nonatomic, strong) UILabel *titleLB; // 标题

@end

@implementation AddStoreNumView
@synthesize reduceNumButton, addNumButton, numTextField;

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

/** 初始化视图 */
- (void)initView {
    self.backgroundColor = [UIColor clearColor];
    
//    [self addSubview:self.lineLabel];
    [self addSubview:self.titleLB];
    [self addSubview:self.reduceNumButton];
    [self addSubview:self.addNumButton];
    [self addSubview:self.numTextField];
}

/** 改变加号按钮色彩 */
- (void)changeAddNumButtonColor:(BOOL)isChangeColor {
    if (isChangeColor) {
        [addNumButton setTitleColor:Color217 forState:UIControlStateNormal];
        [addNumButton roundBorderWithBorderDirection:BorderDirectionRight | BorderDirectionTop | BorderDirecionBottom lineWidth:0.5 color:Color217];
    } else {
        [addNumButton setTitleColor:Color34 forState:UIControlStateNormal];
        [addNumButton roundBorderWithBorderDirection:BorderDirectionRight | BorderDirectionTop | BorderDirecionBottom lineWidth:0.5 color:Color34];
    }
}
/** 改变减号按钮色彩 */
- (void)changeReduceNumButtonColor:(BOOL)isChangeColor {
    if (isChangeColor) {
        [reduceNumButton setTitleColor:Color217 forState:UIControlStateNormal];
        [reduceNumButton roundBorderWithBorderDirection:BorderDirectionLeft | BorderDirectionTop | BorderDirecionBottom lineWidth:0.5 color:Color217];
    } else {
        [reduceNumButton setTitleColor:Color34 forState:UIControlStateNormal];
        [reduceNumButton roundBorderWithBorderDirection:BorderDirectionLeft | BorderDirectionTop | BorderDirecionBottom lineWidth:0.5 color:Color34];
    }
}


#pragma mark - <UITextFieldDelegate>
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([textField.text integerValue] >= maxNum) {
        textField.text = [NSString stringWithFormat:@"%@", @(maxNum)];
        [self changeAddNumButtonColor:YES];
        [self changeReduceNumButtonColor:NO];
    } else {
        [self changeAddNumButtonColor:NO];
        if ([textField.text integerValue] <= 1) {
            textField.text = @"1";
            [self changeReduceNumButtonColor:YES];
        }
    }
    [self didSelectStoreNumText:textField.text];
}

- (void)didSelectStoreNumText:(NSString *)numString {
    if ([self.delegate respondsToSelector:@selector(didSelectedReduceNumOrAddButton:)]) {
        [self.delegate didSelectedReduceNumOrAddButton:numString];
    }
}


#pragma mark - progress
- (void)progressAddOrReduceNumButton:(UIButton *)sender {
    switch (sender.tag) {
        case reduceNumButtonType:
            [self reduceNumMethods];
            break;
        case addNumButtonType:
            [self addNumMethods];
            break;
        default:
            break;
    }
}

/** 减少数量按钮时间监听 */
- (void)reduceNumMethods {
    if ([numTextField.text integerValue] > 1) {
        numTextField.text = [NSString stringWithFormat:@"%@", @([numTextField.text integerValue] -1)];
        [self didSelectStoreNumText:numTextField.text];
        if ([numTextField.text integerValue] <= 1) { // 减号按钮色彩改变
            [self changeReduceNumButtonColor:YES];
        }
        if ([numTextField.text integerValue] < maxNum) { // 加号按钮色彩改变
            [self changeAddNumButtonColor:NO];
        }
    }
    
}

/** 增加数量按钮事件监听 */
- (void)addNumMethods {
    if ([numTextField.text integerValue] < maxNum) {
        numTextField.text = [NSString stringWithFormat:@"%@", @([numTextField.text integerValue] +1)];
        [self didSelectStoreNumText:numTextField.text];
        if ([numTextField.text integerValue] >= maxNum) { // 加号按钮色彩改变
            [self changeAddNumButtonColor:YES];
        }
        if ([numTextField.text integerValue] > 1) { // 减号按钮色彩改变
            [self changeReduceNumButtonColor:NO];
        }
    }
}


#pragma mark - setting
- (void)setInventory:(NSString *)inventory {
    _inventory = inventory;
}


#pragma mark - 懒加载
/** 线 */
- (UILabel *)lineLabel {
    if (!_lineLabel) {
        _lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.5)];
        _lineLabel.backgroundColor = [UIColor lightGrayColor];
    }
    return _lineLabel;
}
/** 标题 */
- (UILabel *)titleLB {
    if (!_titleLB) {
        _titleLB = [[UILabel alloc] initWithFrame:CGRectMake(14, 6, ScreenWidth, 25)];
        _titleLB.text = @"数量";//[@"数量（限购" stringByAppendingString:[NSString stringWithFormat:@"%d件）", maxNum]];
        _titleLB.font = APPFONT(14);
        _titleLB.textColor = RGBACOLOR(44, 44, 44, 1);
    }
    return _titleLB;
}
/** 减少数量按钮 */
- (UIButton *)reduceNumButton {
    if (!reduceNumButton) {
        reduceNumButton = [UIButton buttonWithType:UIButtonTypeCustom];
        reduceNumButton.frame = CGRectMake(15, 38, 47, 32);
        reduceNumButton.titleLabel.font = APPFONT(16);
        [reduceNumButton setTitle:@"-" forState:UIControlStateNormal];
        [reduceNumButton setTitleColor:Color217 forState:UIControlStateNormal];
        [reduceNumButton roundBorderWithBorderDirection:BorderDirectionLeft | BorderDirectionTop | BorderDirecionBottom lineWidth:0.5 color:Color217];
        reduceNumButton.tag = reduceNumButtonType;
        [reduceNumButton addTarget:self action:@selector(progressAddOrReduceNumButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return reduceNumButton;
}
/** 增加数量按钮 */
- (UIButton *)addNumButton {
    if (!addNumButton) {
        addNumButton = [UIButton buttonWithType:UIButtonTypeCustom];
        addNumButton.frame = CGRectMake(126, 38, 47, 32);
        addNumButton.titleLabel.font = APPFONT(16);
        [addNumButton setTitle:@"+" forState:UIControlStateNormal];
        [addNumButton setTitleColor:Color34 forState:UIControlStateNormal];
        [addNumButton roundBorderWithBorderDirection:BorderDirectionRight | BorderDirectionTop | BorderDirecionBottom lineWidth:0.5 color:Color34];
        addNumButton.tag = addNumButtonType;
        [addNumButton addTarget:self action:@selector(progressAddOrReduceNumButton:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return addNumButton;
}
/** 输入数字输入框 */
- (NumberKeyboardAndToolTextField *)numTextField {
    if (!numTextField) {
        numTextField = [[NumberKeyboardAndToolTextField alloc] init];
        numTextField.frame = CGRectMake(62, 38, 64, 32);
        numTextField.font = APPFONT(15);
        numTextField.text = @"1";
        numTextField.textAlignment = NSTextAlignmentCenter;
        [numTextField roundBorderWithBorderDirection:BorderDirectionALL lineWidth:0.5 color:Color34];
        numTextField.delegate = self;
        numTextField.keyboardType = UIKeyboardTypeNumberPad;
//        [numTextField limitTextLength:2];
//        [numTextField initTopView];
        numTextField.block = ^ {
            
        };
    }
    
    return numTextField;
}

@end
